package com.ikun.yummy.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.ikun.yummy.R;
import com.ikun.yummy.bean.CookInfo;
import com.ikun.yummy.bean.OnLikeClickListener;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.util.List;
public class CookListAdapter extends ArrayAdapter<CookInfo> {
    private final int resourceId;
    private final OnLikeClickListener likeclick;
    private int type=1;

    public CookListAdapter(Context context, int textViewResourceId, List<CookInfo> objects,int type, OnLikeClickListener likeclick){
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
        this.likeclick=likeclick;
        this.type=type;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        CookInfo item=getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView==null){
            view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder= new ViewHolder();
            viewHolder.cover=view.findViewById(R.id.item_home_cook_cover);
            viewHolder.cookname=view.findViewById(R.id.item_home_cookname);
            viewHolder.ingredients=view.findViewById(R.id.item_home_cook_ingredients);
            viewHolder.username=view.findViewById(R.id.item_home_cook_username);
            viewHolder.time=view.findViewById(R.id.item_home_cook_time);
            if (type!=2){
                viewHolder.like=view.findViewById(R.id.item_home_cook_like);
            }
            view.setTag(viewHolder);
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }
        if (likeclick!=null&&viewHolder.like!=null) {
            viewHolder.like.setOnClickListener(v -> likeclick.like(v, item, position));
        }
        File file=new File(item.coverimage);
        if (file.exists()){
            viewHolder.cover.setImageBitmap(BitmapFactory.decodeFile(item.coverimage));
        }else{
            viewHolder.cover.setImageResource(R.drawable.app_logo);
        }
        viewHolder.cookname.setText(item.cookname);
        StringBuilder ing= new StringBuilder();
        try {
            JSONArray arr=new JSONArray(item.ingredients);
            for (int i = 0; i < arr.length(); i++) {
                ing.append(arr.getString(i)).append(",");
            }
        } catch (JSONException ignored) {}
        viewHolder.ingredients.setText(ing.toString());
        viewHolder.username.setText(item.username);
        viewHolder.time.setText(item.time);

        if (type==1 && viewHolder.like!=null){
            if (item.like){
                viewHolder.like.setImageResource(R.mipmap.liked);
            }else{
                viewHolder.like.setImageResource(R.mipmap.like);
            }
        }
        return view;
    }

    static class ViewHolder{
        ImageView cover,like;
        TextView cookname,ingredients,username,time;
    }
}